param(
	[Parameter(Mandatory=$True)]
	[string]$csv1,                  # the 1st csv file to be managed
	[Parameter(Mandatory=$True)]    #
	[string]$csv2,                  # the 2nd csv file to be managed
	[Parameter(Mandatory=$false)]    #
	[string]$key="IE"               # the Category of csv file
)
$this_path = Split-Path -Parent ($MyInvocation.MyCommand.Definition)
$csv_file1="$this_path\$csv1"
$csv_file2="$this_path\$csv2"
$total_csv_info=[ordered]@{}
#<------------------------------ 1st csv ----------------------------------------
$csv_text_content1=(Get-Content $csv_file1 -ReadCount 0) -replace('\x00','') # remove nul in the text file
$csv_csv_content1=ipcsv $csv_file1
$csv_header_count1=($csv_csv_content1|gm -MemberType NoteProperty).count
[string[]]$csv_settings1+='"setting"'
$csv_csv_content1.setting|%{$csv_settings1+="`"$_`""} # get all 1st csv registries
#<------------------------------ 2nd csv ---------------------------------------- 
$csv_text_content2=(Get-Content $csv_file2 -ReadCount 0) -replace('\x00','') # remove nul in the text file
$csv_csv_content2=ipcsv $csv_file2
$csv_header_count2=($csv_csv_content2|gm -MemberType NoteProperty).count
[string[]]$csv_settings2+='"setting"'
$csv_csv_content2.setting|%{$csv_settings2+="`"$_`""} # get all 2nd csv registries
#<----------- add each line from 1st csv to the total ie hash table -------------------
For( $i=0; $i -lt $csv_settings1.count; $i++ ){
	$total_csv_info.Add($csv_settings1[$i],$csv_text_content1[$i])
}
#<--- add (,"N/A") at end of the line if 1sr csv settings not in 2nd csv file settings ---
Foreach( $line in (compare $csv_settings1 $csv_settings2|?{$_.SideIndicator -eq "<="}).InputObject){
	$total_csv_info.$line+=',"N/A"' * ($csv_header_count2 - 2) 
}
#<----------------- loop each setting of 2nd csv file -----------------
For( $i=0; $i -lt $csv_settings2.count; $i++ ){
	$this_setting=$csv_settings2[$i] # get registry key and value
	$this_line = $csv_text_content2[$i] -split(',"')
	$this_setting_value=[string]::Join(',',($this_line[(2 .. ($this_line.count-1))]|%{'"' + $_}))
	If($this_setting -in $csv_settings1 ){ # if the setting in 2nd csv is in 1st csv settings
		$total_csv_info.$this_setting+=",$this_setting_value" 
	}else{$total_csv_info.Add($this_setting,"`"$key`",$this_setting,"+'"N/A",' * ($csv_header_count1 - 2) +$this_setting_value) }
}
[string[]]$total_csv_info.Values|out-file -FilePath $csv_file1 -Encoding utf8 -Force
del $csv_file2 -Force